<?php
/**
 * 销售物料Lib类
 * @author xiayuanjun@shopex.cn
 * @version 1.0
 */
class material_sales_material{

    function __construct(){
        $this->_salesMaterialObj = app::get('material')->model('sales_material');
        $this->_salesBasicMaterialObj = app::get('material')->model('sales_basic_material');
        $this->_basicMaterialObj = app::get('material')->model('basic_material');
        $this->_basicMaterialExtObj = app::get('material')->model('basic_material_ext');
        $this->_basicMaterialStore  = app::get('ome')->model('branch_product');
    }

    /**
     *
     * 根据来源店铺及销售物料货号获取销售物料信息
     * @param String $shop_id
     * @param String $bn
     * @return Boolean
     */
    public function getSalesMByBn($shop_id, $bn){
        $materia_bn = trim($bn);
        $salesMaterialInfo = $this->_salesMaterialObj->getList('sm_id,sales_material_name,sales_material_bn,shop_id,is_bind,sales_material_type',array('sales_material_bn'=>$materia_bn,'shop_id'=>array($shop_id,'_ALL_')), 0, 1);
        if($salesMaterialInfo){
            return $salesMaterialInfo[0];
        }
        return false;
    }

    /**
     *
     * 根据来源店铺及销售物料ID获取销售物料信息
     * @param String $shop_id
     * @param Int $sm_id
     * @return Boolean
     */
    public function getSalesMById($shop_id, $sm_id){
        $salesMaterialInfo = $this->_salesMaterialObj->getList('sm_id,sales_material_name,sales_material_bn,shop_id,is_bind,sales_material_type',array('sm_id'=>$sm_id,'shop_id'=>array($shop_id,'_ALL_')), 0, 1);
        if($salesMaterialInfo){
            return $salesMaterialInfo[0];
        }
        return false;
    }

    /**
     *
     * 根据来源店铺及货号获取销售物料信息
     * @param String $shop_id
     * @param String $bn
     * @return Boolean
     */
    public function getBasicMBySalesMId($sm_id){
        $salesBasicMInfos = $this->_salesBasicMaterialObj->getList('bm_id,sm_id,number,rate',array('sm_id'=>$sm_id), 0, -1);
        if($salesBasicMInfos){
            foreach($salesBasicMInfos as $k => $salesBasicMInfo){
                $bmIds[] = $salesBasicMInfo['bm_id'];
                $bmAndSmRates[$salesBasicMInfo['bm_id']] = $salesBasicMInfo;
            }

            $basicMaterialExtInfos = $this->_basicMaterialExtObj->getList('bm_id,cost,weight,retail_price,unit',array('bm_id'=>$bmIds), 0, -1);
            if($basicMaterialExtInfos){
                foreach($basicMaterialExtInfos as $key => $basicMaterialExtInfo){
                    $bmExts[$basicMaterialExtInfo['bm_id']] = $basicMaterialExtInfo;
                }
            }

            $basicMaterialInfos = $this->_basicMaterialObj->getList('bm_id,material_name,material_bn',array('bm_id'=>$bmIds), 0, -1);
            if($basicMaterialInfos){
                foreach($basicMaterialInfos as $key => $basicMaterialInfo){
                    $bmList[] = array_merge($basicMaterialInfo, $bmAndSmRates[$basicMaterialInfo['bm_id']], $bmExts[$basicMaterialInfo['bm_id']]);
                }
                return $bmList;
            }
        }
        return false;
    }

    /**
     *
     * 根据促销总价格计算每个物料的贡献金额值
     * @param String $shop_id
     * @param String $bn
     * @return Boolean
     */
    public function calProSaleMPriceByRate($sale_price, &$bm_bns){
        if($sale_price <=0){
            foreach($bm_bns as $k =>$bm_bn){
                $bm_bns[$k]['rate_price'] = 0.00;
            }
            return true;
        }

        $less_price = $sale_price;
        $count_sku = count($bm_bns);
        $i = 1;
        foreach($bm_bns as $k =>$bm_bn){
            if($i == $count_sku){
                $bm_bns[$k]['rate_price'] = $less_price;
            }else{
                $tmp_rate = $bm_bn['rate']/100;
                $bm_bns[$k]['rate_price'] = bcmul($sale_price, $tmp_rate, 2);
                $less_price -= $bm_bns[$k]['rate_price'];
            }
            $i++;
        }

        return true;
    }

    /**
     * @param $sm_id
     * @param $salse_num
     * @return bool
     */
    public function validSalesBasicMaterial($sm_id, $salse_num){
        $relations = $this->_salesBasicMaterialObj->getList('sm_id, bm_id, number', array('sm_id'=>$sm_id), 0, -1);
        foreach($relations as $k => $v){
            $all_nums = $salse_num * $v['number'];
            $stores = $this->_basicMaterialStore->dump(array('product_id' => $v['bm_id']));
            if($stores['store'] - $all_nums < 0){
                return false;
            }
        }
        return true;

    }

    /**
     *
     * 根据来源店铺及货号获取促销[捆绑]销售物料信息
     * @param String $shop_id
     * @param String $bn
     * @return Boolean
     */
    public function getBasicMBySalesMIds($sm_id)
    {
        $salesBasicMInfos = $this->_salesBasicMaterialObj->getList('sm_id, bm_id,sm_id,number,rate',array('sm_id'=>$sm_id), 0, -1);
        if($salesBasicMInfos)
        {
            foreach($salesBasicMInfos as $k => $salesBasicMInfo)
            {
                $bmIds[] = $salesBasicMInfo['bm_id'];
                $bmAndSmRates[$salesBasicMInfo['sm_id']][$salesBasicMInfo['bm_id']] = $salesBasicMInfo;
            }
            
            $bmExts    = array();
            $basicMaterialExtInfos = $this->_basicMaterialExtObj->getList('bm_id,cost,weight,retail_price,unit',array('bm_id'=>$bmIds), 0, -1);
            if($basicMaterialExtInfos)
            {
                foreach($basicMaterialExtInfos as $key => $basicMaterialExtInfo)
                {
                    $bmExts[$basicMaterialExtInfo['bm_id']] = $basicMaterialExtInfo;
                }
            }
            
            $basic_material_infos    = array();
            $basicMaterialInfos = $this->_basicMaterialObj->getList('bm_id,material_name,material_bn',array('bm_id'=>$bmIds), 0, -1);
            if($basicMaterialInfos)
            {
                foreach($basicMaterialInfos as $key => $basicMaterialInfo)
                {
                    $basic_material_infos[$basicMaterialInfo['bm_id']]    = $basicMaterialInfo;
                }
            }
            
            #合并信息
            $bmList    = array();
            foreach ($bmAndSmRates as $sm_key => $sales_basic_material_list)
            {
                foreach ($sales_basic_material_list as $bm_key => $bm_item)
                {
                    $bmList[$sm_key][]    = array_merge($basic_material_infos[$bm_key], $bm_item, $bmExts[$bm_key]);
                }
            }
            
            return $bmList;
        }
        return false;
    }
}
